<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>台站列表</title>
    <link rel="stylesheet" th:href="@{../../component/pear/css/pear.css}"/>
</head>

<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <div class="col-sm-13 search-collapse">
            <form class="layui-form" id="formId">
                <div class="layui-form-item">
                    <label class="layui-form-label">台站ID：</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" lay-required name="code" placeholder="请输入台站ID" type="text"/>
                    </div>

                    <label class="layui-form-label" style="width: 7%">台站名称：</label>
                    <div class="layui-input-inline">
                        <input class="layui-input" lay-required name="name" placeholder="请输入台站名称" type="text"/>
                    </div>
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-filter="myTable-query" lay-submit>
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button class="pear-btn pear-btn-md" type="reset">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="myTable" lay-filter="myTable"></table>
    </div>
</div>

<script th:src="@{../../component/layui/layui.js}"></script>
<script th:src="@{../../component/pear/pear.js}"></script>

<script type="text/html" id="power-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>
</script>
<script>
    layui.use(['table', 'form', 'jquery', 'popup'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let popup = layui.popup;
        let MODULE_PATH = "/system/station/";

        setInterval(function () {
            table.reloadData('myTable');
        }, 10000);

        form.on('submit(myTable-query)', function (data) {
            table.reload('myTable', {where: data.field})
            return false;
        });

        let cols = [
            [
                {
                    title: '台站ID',
                    field: 'code',
                    align: 'center'
                },
                {
                    title: '台站名称',
                    field: 'name',
                    align: 'center'
                },
                {
                    title: '台站地址',
                    field: 'address',
                    align: 'center'
                },
                {
                    title: '经度',
                    field: 'longitude',
                    align: 'center'
                },
                {
                    title: '纬度',
                    field: 'latitude',
                    align: 'center'
                },
                {
                    title: '所属区域',
                    field: 'region',
                    align: 'center'
                },
                {
                    title: '操作',
                    toolbar: '#barDemo',
                    width: '15%'
                }
            ]
        ]

        table.render({
            elem: '#myTable',
            url: MODULE_PATH +'data',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#power-toolbar',
            defaultToolbar: [],
            cellMinWidth: 'auto',
            done: function (res, curr, count) {
            }
        });

        table.on('toolbar(myTable)', function(obj){
            if(obj.event === 'add'){
                var index = layer.open({
                    type: 2,
                    title: "添加台站",
                    fix: false,
                    maxmin: true,
                    shadeClose: true,
                    area: ['1000px', '525px'],
                    shade: 0.4,
                    skin: 'layui-layer-rim',
                    content: ['/system/view/station_add', "no"],
                    success: function (layero, index) {
                    }
                });
            }
        });

        table.on('tool(myTable)', function(obj) {
            var rowIndex = $(obj.tr).attr("data-index");
            var data = obj.data;
            json = JSON.stringify(data);
            switch(obj.event) {
                case 'delete':
                    layer.confirm("确认要删除吗，删除后不能恢复", { title: "删除确认" }, function (index) {
                        $.ajax({
                            url: MODULE_PATH +"delStation",
                            type: 'POST',
                            contentType: 'application/json;charset=UTF-8',
                            data: json,
                            success: function (data) {
                                if (data.code === 200) {
                                    popup.success(data.msg);
                                    table.reloadData('myTable');
                                } else {
                                    popup.failure(data.msg);
                                    table.reloadData('myTable');
                                }
                            },
                            error: function () {
                                popup.failure("删除失败!")
                            }
                        });
                    });
                    break;
            }
        });
    })

</script>
</body>

</html>
